package com.ikas.ai.server.kafka.strategy;

import com.ikas.ai.enums.MeteTypeEnum;
import com.ikas.ai.model.KafkaMeteData;
import com.ikas.ai.server.module.data.model.BaseMete;
import com.ikas.ai.server.module.data.model.dto.Kafka104MeteData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hu.bowei@ikasinfo.com
 * @date 2023-11-06- 11:22
 */
@Component("BaseMeteStrategy")
@Slf4j
public class BaseMeteStrategy implements Kafka104DataConvertMeteDataStrategy {
    @Override
    public List<KafkaMeteData> doOperation(List<Kafka104MeteData> kafka104MeteDataList, Map meteMap) {

        return kafka104MeteDataList.stream().map(item -> {
            KafkaMeteData kafkaMeteData = new KafkaMeteData();
            kafkaMeteData.setGenerateTime(item.getGenerateTime());
            kafkaMeteData.setGenerateDate(item.getGenerateDate());
            BaseMete baseMete = (BaseMete) meteMap.get(item.getAddress());
            if (baseMete == null) {
                log.info("baseMete is null,mete address:{}", item.getAddress());
                return null;
            }
            kafkaMeteData.setMachineNo(baseMete.getMachineNo());
            kafkaMeteData.setMeteId(baseMete.getMeteId());
            kafkaMeteData.setMeteCode(baseMete.getMeteCode());
            kafkaMeteData.setMeteName(baseMete.getSignalCnName());
            /**
             * 添加model_use相关修改
             */
            if (baseMete.getModelUse()==null) {
                    kafkaMeteData.setModelUse(0);
            }
            kafkaMeteData.setModelUse(baseMete.getModelUse());
            kafkaMeteData.setMeteType(MeteTypeEnum.convert(item.getCategoryType()).getNum());
            String valueStr = item.getCategoryValue();
            if ("0.0".equalsIgnoreCase(valueStr)) {
                kafkaMeteData.setValue("0");
            } else if ("1.0".equalsIgnoreCase(valueStr)) {
                kafkaMeteData.setValue("1");
            } else {
                kafkaMeteData.setValue(valueStr);
            }
            return kafkaMeteData;
        }).collect(Collectors.toList());
    }
}
